
from models import Model

from datetime import datetime
from dateutil.relativedelta import relativedelta
from collections import OrderedDict


class ReportModel(Model):
    def report_print2(self, values, context=None):
        return self.report_print(values, context=context)

    def report_month(self, date_month, context=None):
        values = self.default_get_onchange(context=context)

        date = datetime.strptime(date_month, '%Y-%m-%d')
        date_from = datetime(date.year, date.month, 1)
        date_to = date_from + relativedelta(months=+1, days=-1)
        date_from = date_from.strftime('%Y-%m-%d')
        date_to = date_to.strftime('%Y-%m-%d')

        values_input = {'date_from': date_from, 'date_to': date_to}
        values.update(values_input)
        res = self.report_print2(values, context=context)
        return res


class AccountingReport(ReportModel):
    _name = "accounting.report"

    def report_print222222222(self, values, context=None):
        callback = self.report_print

        print('values', values, context)

        balancesheet_account_report_id = self.env.ref(
            'accounting_pdf_reports.account_financial_report_balancesheet0')

        balancesheet_account_report_id = balancesheet_account_report_id[1]

        if values.get('account_report_id') != balancesheet_account_report_id:
            return callback(values, context=context)

        date_from = values.get('date_from')

        date_to_open = datetime.strptime(
            date_from, '%Y-%m-%d') + relativedelta(days=-1)
        date_to_open = date_to_open.strftime('%Y-%m-%d')

        values_open = values.copy()
        values_open['date_from'] = None
        values_open['date_to'] = date_to_open
        res_open = callback(values_open, context=context)

        values_close = values.copy()
        values_close['date_from'] = None
        res_close = callback(values_close, context=context)
        res_delta = callback(values, context=context)

        key_names = {
            'report,sum,Balance Sheet': '总计',
            'report,sum,Liability': '负债及损益',
            'report,account_report,Profit (Loss) to report': '损益',
            'report,account_type,Assets': '资产',
            'report,account_type,Assets,accounts': '',
            'report,account_type,Liability': '负债',
            'report,account_type,Liability,accounts': '',
            'step': 'report,sum,Balance Sheet'
        }

        def to_dict(res):
            result = dict((key, {}) for key in key_names.keys())

            for line in res.get('get_account_lines'):
                full_name = '%s,%s,%s' % (
                    line['type'], line['account_type'], line['name'])

                if full_name in result.keys():
                    result['step'] = full_name
                    result[full_name] = line
                else:
                    step = result['step']
                    accounts = result[step + ',accounts']
                    accounts[line['name'][0:6]] = line

            return result

        dict_open = to_dict(res_open)
        dict_close = to_dict(res_close)
        dict_delta = to_dict(res_delta)

        # print('values_open---,',  values_open)
        # print('res_open---,',  res_open)
        # print('dict_open---,', dict_open)
        # print('res_close---,',  res_close)
        # print('dict_close---,', dict_close)
        # print('res_delta---,',  res_delta)
        # print('dict_delta---,', dict_delta)

        def merge_sum(name):
            open1 = dict_open[name]
            close = dict_close[name]
            delta = dict_delta[name]
            return {
                'name2': key_names[name],
                'name': open1['name'],
                'type': open1['type'],
                'account_type': open1['account_type'],
                'level': open1['level'],
                'debit_open': open1['balance'] > 0 and open1['balance'] or 0,
                'credit_open': open1['balance'] < 0 and -open1['balance'] or 0,
                'debit': delta['debit'],
                'credit':  delta['credit'],
                'debit_close': close['balance'] > 0 and close['balance'] or 0,
                'credit_close': close['balance'] < 0 and -close['balance'] or 0,
            }

        def merge_account(name):
            open1 = dict_open.get(name, {})
            close = dict_close.get(name, {})
            delta = dict_delta.get(name, {})

            keys = list(open1.keys()) + list(close.keys()) + list(delta.keys())
            keys = list(set(keys))
            keys.sort()

            def fn1(k):
                c3 = close[k]
                c3_bal = c3['balance']
                o3_bal = open1.get(k, {'balance': 0})['balance']
                d3 = delta.get(k, {'debit': 0, 'credit': 0})
                return {
                    'name2': c3['name'],
                    'name': c3['name'],
                    'type': c3['type'],
                    'account_type': c3['account_type'],
                    'level': c3['level'],
                    'debit_open': o3_bal > 0 and o3_bal or 0,
                    'credit_open': o3_bal < 0 and -o3_bal or 0,
                    'debit': d3['debit'],
                    'credit':  d3['credit'],
                    'debit_close': c3_bal > 0 and c3_bal or 0,
                    'credit_close': c3_bal < 0 and -c3_bal or 0,
                }

            return [fn1(k) for k in keys]

            # 'report,sum,Balance Sheet': {},
            # 'report,account_type,Assets': {},
            # 'report,account_type,Assets,accounts': {},

            # 'report,sum,Liability': {},
            # 'report,account_type,Liability': {},
            # 'report,account_type,Liability,accounts': {},

            # 'report,account_report,Profit (Loss) to report': {},

        assets_accounts = 'report,account_type,Assets,accounts'
        liability_accounts = 'report,account_type,Liability,accounts'

        res_list = []
        # res_list.append(merge_sum('report,sum,Balance Sheet'))
        res_list.append(merge_sum('report,account_type,Assets'))
        res_list = res_list + merge_account(assets_accounts)
        res_list.append(merge_sum('report,sum,Liability'))
        res_list.append(merge_sum('report,account_type,Liability'))
        res_list = res_list + merge_account(liability_accounts)
        res_list.append(
            merge_sum('report,account_report,Profit (Loss) to report'))

        res_ret = res_delta.copy()
        res_ret['get_account_lines'] = res_list
        print('res_ret', res_ret)
        return res_ret

        # callback = self.report_print
        # return callback(values, context=context)

    # def report_print1(self, values, context=None):
    #     return super(AccountingReport, self).report_print(values, context=context)

    def default_get_onchange(self, context=None):
        # default_account_report_id = self.env.ref(
        #     'accounting_pdf_reports.account_financial_report_balancesheet0')
        # context = {
        #     'default_account_report_id': default_account_report_id[1]  # 余额表
        # }

        # default_account_report_id = self.env.ref(
        #     'accounting_pdf_reports.account_financial_report_profitandloss0')

        # context = {
        #     'default_account_report_id': default_account_report_id[1]  # 余额表
        # }

        values = super(AccountingReport, self).default_get_onchange(
            context=context)
        values.update({'debit_credit': True})
        return values

    def test_report(self):
        self.test_report_month()
        # self.test_report2()

    def test_report_month(self):
        res = self.report_month('2020-10-1')
        print('res-----:', res)

    def test_report2(self):
        context = {}
        values = self.default_get_onchange(context=context)

        date_from = '2020-1-1'
        date_to = '2020-10-30'
        values_input = {'date_from': date_from, 'date_to': date_to}
        values.update(values_input)

        res = self.report_print2(values, context=context)
        # print('res:', res)
        print('---- res:', )
        for k, v in res.items():
            print(k, v)

        res = {
            'doc_ids': [], 'doc_model': 'accounting.report',
            'data': {
                'id': 22, 'date_from': '2020-01-01', 'date_to': '2020-10-30',
                'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'target_move': 'posted', 'company_id': [1, 'My Company'],
                'used_context': {'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'state': 'posted', 'date_from': '2020-01-01', 'date_to': '2020-10-30', 'strict_range': True, 'company_id': 1, 'lang': 'zh_CN'},
                'date_from_cmp': False, 'debit_credit': False, 'date_to_cmp': False,
                'filter_cmp': 'filter_no',
                'account_report_id': [4, 'Balance Sheet'],
                'enable_filter': False,
                'label_filter': False,
                'comparison_context': {'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'state': 'posted'}
            },
            'docs': 'accounting.report(22,)',
            'time': "<module 'time' (built-in)>",
            'get_account_lines': [
                {'name': 'Balance Sheet', 'balance': 0.0,
                         'type': 'report', 'level': '0', 'account_type': 'sum'},
                {'name': 'Assets', 'balance': 6990.0, 'type': 'report',
                 'level': '0', 'account_type': 'account_type'},
                {'name': '100101 现金', 'balance': 1200.0, 'type': 'account',
                 'level': 4, 'account_type': 'liquidity'},
                {'name': '100201 银行', 'balance': 1190.0, 'type': 'account',
                 'level': 4, 'account_type': 'liquidity'},
                {'name': '112200 应收账款', 'balance': 4600.0, 'type': 'account',
                 'level': 4, 'account_type': 'receivable'},
                {'name': 'Liability', 'balance': -6990.0,
                 'type': 'report', 'level': '0', 'account_type': 'sum'},
                {'name': 'Liability', 'balance': 2100.0, 'type': 'report',
                 'level': '0', 'account_type': 'account_type'},
                {'name': '220200 应付账款', 'balance': 2100.0, 'type': 'account',
                 'level': 4, 'account_type': 'payable'},
                {'name': 'Profit (Loss) to report', 'balance': -9090.0,
                         'type': 'report', 'level': '0', 'account_type': 'account_report'}
            ]
        }


class AccountingReportBalancesheet(AccountingReport):
    _name = "accounting.report.balancesheet"
    _inherit = "accounting.report"

    def default_get_onchange(self, context=None):
        default_account_report_id = self.env.ref(
            'accounting_pdf_reports.account_financial_report_balancesheet0')
        context = {
            'default_account_report_id': default_account_report_id[1]  # 余额表
        }

        values = super(AccountingReportBalancesheet,
                       self).default_get_onchange(context=context)
        return values


class AccountingReportProfitandloss(AccountingReport):
    _name = "accounting.report.profitandloss"
    _inherit = "accounting.report"

    def default_get_onchange(self, context=None):
        default_account_report_id = self.env.ref(
            'accounting_pdf_reports.account_financial_report_profitandloss0')

        context = {
            'default_account_report_id': default_account_report_id[1]  # 余额表
        }

        values = super(AccountingReportProfitandloss,
                       self).default_get_onchange(context=context)
        return values


class AccountReportGeneralLedger(ReportModel):
    _name = "account.report.general.ledger"

    def default_get_onchange(self, context=None):
        values = super(AccountReportGeneralLedger,
                       self).default_get_onchange(context=context)
        values.update({'initial_balance': True})
        return values

    def test_report(self):
        self.test_report_month()
        # self.test_report2()

    def test_report_month(self):
        res = self.report_month('2020-10-1')
        print('----res:', res)

    def test_report2(self):
        context = {}
        values = self.default_get_onchange(context=context)

        date_from = '2020-10-1'
        date_to = '2020-10-30'
        values_input = {'date_from': date_from, 'date_to': date_to}
        values.update(values_input)

        res = self.report_print2(values, context=context)
        # print('res:', res)
        print('---- res:', )
        for k, v in res.items():
            print(k, v)

        res = {'doc_ids': None, 'doc_model': 'account.report.general.ledger',
               'data': {'id': 28, 'date_from': '2020-10-01', 'date_to': '2020-10-30',
                        'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'target_move': 'posted',
                        'company_id': [1, 'My Company'],
                        'used_context': {'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'state': 'posted',
                                         'date_from': '2020-10-01', 'date_to': '2020-10-30', 'strict_range': True,
                                         'company_id': 1, 'lang': 'zh_CN'},
                        'display_account': 'movement', 'initial_balance': True,
                        'sortby': 'sort_date'},
               'docs': 'account.report.general.ledger(28,)',
               'time': "<module 'time' (built-in)>",
               'Accounts': [{'credit': 0.0, 'debit': 1200.0, 'balance': 1200.0, 'code': '100101', 'name': '现金',
                             'move_lines': [
                                 {'lid': 0, 'ldate': '', 'lcode': '', 'amount_currency': 0.0, 'lref': '',
                                  'lname': 'Initial Balance',
                                  'debit': 1200.0, 'credit': 0.0, 'balance': 1200.0,
                                  'lpartner_id': '', 'move_name': '', 'mmove_id': '',
                                  'currency_code': '', 'currency_id': None, 'invoice_id': '',
                                  'invoice_type': '', 'invoice_number': '', 'partner_name': ''}]
                             },
                            {'credit': 2100.0, 'debit': 3290.0, 'balance': 1190.0, 'code': '100201', 'name': '银行',
                             'move_lines': [
                                 {'lid': 0, 'ldate': '', 'lcode': '', 'amount_currency': 0.0,
                                  'lref': '', 'lname': 'Initial Balance',
                                  'debit': 1000.0, 'credit': 2100.0, 'balance': -1100.0,
                                  'lpartner_id': '',
                                  'move_name': '', 'mmove_id': '', 'currency_code': '', 'currency_id': None, 'invoice_id': '', 'invoice_type': '', 'invoice_number': '',
                                  'partner_name': ''},
                                 {'lid': 12, 'ldate': '2020-10-01', 'lcode': 'BNK1', 'currency_id': None, 'amount_currency': 0.0,
                                  'lref': None, 'lname': 'CUST.IN/2020/0001',
                                  'debit': 90.0, 'credit': 0.0, 'balance': -1010.0,
                                  'move_name': 'BNK1/2020/0001', 'currency_code': None,
                                  'partner_name': '别克'},
                                 {'lid': 14, 'ldate': '2020-10-02', 'lcode': 'BNK1', 'currency_id': None, 'amount_currency': 0.0,
                                  'lref': None, 'lname': 'CUST.IN/2020/0002',
                                  'debit': 2200.0, 'credit': 0.0, 'balance': 1190.0,
                                  'move_name': 'BNK1/2020/0002', 'currency_code': None,
                                  'partner_name': '宝马汽车'}]
                             },
                            {'credit': 4490.0, 'debit': 9090.0, 'balance': 4600.0, 'code': '112200', 'name': '应收账款',
                             'move_lines': [
                                 {'lid': 0, 'ldate': '', 'lcode': '', 'amount_currency': 0.0,
                                  'lref': '', 'lname': 'Initial Balance',
                                  'debit': 0.0, 'credit': 2200.0, 'balance': -2200.0,
                                  'lpartner_id': '',
                                  'move_name': '', 'mmove_id': '', 'currency_code': '', 'currency_id': None, 'invoice_id': '', 'invoice_type': '', 'invoice_number': '',
                                  'partner_name': ''},
                                 {'lid': 2, 'ldate': '2020-10-01', 'lcode': 'INV', 'currency_id': None, 'amount_currency': 0.0,
                                  'lref': '', 'lname': 'INV/2020/0001',
                                  'debit': 90.0, 'credit': 0.0, 'balance': -2110.0,
                                  'move_name': 'INV/2020/0001', 'currency_code': None,
                                  'partner_name': '别克'},
                                 {'lid': 11, 'ldate': '2020-10-01', 'lcode': 'BNK1', 'currency_id': None, 'amount_currency': 0.0,
                                  'lref': None, 'lname': '客户付款',
                                  'debit': 0.0, 'credit': 90.0, 'balance': -2200.0,
                                  'move_name': 'BNK1/2020/0001', 'currency_code': None,
                                  'partner_name': '别克'},
                                 {'lid': 13, 'ldate': '2020-10-02', 'lcode': 'BNK1', 'currency_id': None, 'amount_currency': 0.0,
                                  'lref': None, 'lname': '客户付款',
                                  'debit': 0.0, 'credit': 2200.0, 'balance': -4400.0,
                                  'move_name': 'BNK1/2020/0002', 'currency_code': None,
                                  'partner_name': '宝马汽车'},
                                 {'lid': 22, 'ldate': '2020-10-03', 'lcode': 'INV', 'currency_id': None, 'amount_currency': 0.0,
                                  'lref': '', 'lname': 'INV/2020/0002',
                                  'debit': 9000.0, 'credit': 0.0, 'balance': 4600.0,
                                  'move_name': 'INV/2020/0002', 'currency_code': None,
                                  'partner_name': '宝马汽车'}]
                             },
                            {'credit': 0.0, 'debit': 2100.0, 'balance': 2100.0, 'code': '220200', 'name': '应付账款',
                             'move_lines': [
                                 {'lid': 0, 'ldate': '', 'lcode': '', 'amount_currency': 0.0,
                                  'lref': '', 'lname': 'Initial Balance',
                                  'debit': 2100.0, 'credit': 0.0, 'balance': 2100.0,
                                  'lpartner_id': '',
                                  'move_name': '', 'mmove_id': '', 'currency_code': '', 'currency_id': None, 'invoice_id': '', 'invoice_type': '', 'invoice_number': '',
                                  'partner_name': ''}]
                             },
                            {'credit': 9090.0, 'debit': 0.0, 'balance': -9090.0, 'code': '600100', 'name': '主营业务收入',
                             'move_lines': [
                                 {'lid': 1, 'ldate': '2020-10-01', 'lcode': 'INV', 'currency_id': None, 'amount_currency': 0.0,
                                  'lref': '', 'lname': '[cocacola] 可口可乐',
                                  'debit': 0.0, 'credit': 90.0, 'balance': -90.0,
                                  'move_name': 'INV/2020/0001', 'currency_code': None,
                                  'partner_name': '别克'},
                                 {'lid': 21, 'ldate': '2020-10-03', 'lcode': 'INV', 'currency_id': None, 'amount_currency': 0.0,
                                  'lref': '', 'lname': '[cocacola] 可口可乐', 'debit': 0.0, 'credit': 9000.0, 'balance': -9090.0,
                                  'move_name': 'INV/2020/0002', 'currency_code': None,
                                  'partner_name': '宝马汽车'}]
                             }
                            ],

               'print_journal': ['INV', '账单', '杂项', '交易', 'BNK1', 'CSH1', 'CABA']
               }


class AccountReportPartnerLedger(ReportModel):
    _name = 'account.report.partner.ledger'

    def default_get_onchange(self, context=None):
        values = super(AccountReportPartnerLedger,
                       self).default_get_onchange(context=context)
        # result_selection :  customer , supplier, customer_supplier,
        # 应收 / 应付 / 应收及应付

        values2 = {'result_selection': 'customer_supplier'}
        values.update(values2)

        return values

    def test_report(self):
        self.test_report_month()
        # self.test_report2()

    def test_report_month(self):
        res = self.report_month('2020-10-1')
        print('---res---:', res)

    def test_report2(self):
        context = {}
        values = self.default_get_onchange(context=context)

        date_from = '2020-1-1'
        date_to = '2020-12-10'
        values_input = {'date_from': date_from, 'date_to': date_to}
        values.update(values_input)

        res = self.report_print2(values, context=context)
        print('res:', res)

        res = {'doc_ids': [7], 'doc_model': 'res.partner()',
               'data': {'ids': [], 'model': 'ir.ui.menu',
                        'form': {'id': 20, 'date_from': '2020-01-01', 'date_to': '2020-10-10', 'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'target_move': 'posted', 'company_id': [1, 'My Company'],
                                 'used_context': {'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'state': 'posted', 'date_from': '2020-01-01', 'date_to': '2020-10-10', 'strict_range': True, 'company_id': 1, 'lang': 'zh_CN'},
                                 'result_selection': 'customer', 'reconciled': False, 'amount_currency': False},
                        'computed': {'move_state': ['posted'], 'ACCOUNT_TYPE': ['receivable'], 'account_ids': [4, 5, 7, 8, 9, 10]}
                        },
               'docs': ['res.partner(7,)'],
               'time': "<module 'time' (built-in)>",
               'lines': '<bound method ReportPartnerLedger._lines of report.accounting_pdf_reports.report_partnerledger()>',
               'sum_partner': '<bound method ReportPartnerLedger._sum_partner of report.accounting_pdf_reports.report_partnerledger()>',

               'report_data': [{'id': 7, 'partner_id': 7, 'partner_name': '宝马汽车', 'partner_display_name': '宝马汽车',
                                'debit': 0.0, 'credit': 2200.0, 'balance': -2200.0,
                                'lines': [{'id': 13, 'date': '2020-10-02', 'code': 'BNK1', 'a_code': '112200', 'a_name': 'Accounts Receivable', 'ref': None, 'move_name': 'BNK1/2020/0002', 'name': '客户付款',
                                           'debit': 0.0, 'credit': 2200.0, 'amount_currency': 0.0, 'currency_id': 'res.currency()', 'currency_code': None,
                                           'displayed_name': 'BNK1/2020/0002-客户付款', 'progress': -2200.0
                                           }]
                                }]
               }


class AccountReportPartnerLedgerCustomer(AccountReportPartnerLedger):
    _name = "account.report.partner.ledger.customer"
    _inherit = "account.report.partner.ledger"

    def default_get_onchange(self, context=None):
        values = super(AccountReportPartnerLedgerCustomer,
                       self).default_get_onchange(context=context)
        # result_selection :  customer , supplier, customer_supplier,
        # 应收 / 应付 / 应收及应付

        values2 = {'result_selection': 'customer'}
        values.update(values2)

        return values


class AccountReportPartnerLedgerSupplier(AccountReportPartnerLedger):
    _name = "account.report.partner.ledger.supplier"
    _inherit = "account.report.partner.ledger"

    def default_get_onchange(self, context=None):
        values = super(AccountReportPartnerLedgerSupplier,
                       self).default_get_onchange(context=context)
        # result_selection :  customer , supplier, customer_supplier,
        # 应收 / 应付 / 应收及应付

        values2 = {'result_selection': 'supplier'}
        values.update(values2)

        return values


class AccountBalanceReport(ReportModel):
    _name = 'account.balance.report'

    def test_report(self):
        self.test_report_month()
        # self.test_report2()

    def test_report_month(self):
        res = self.report_month('2020-10-1')
        print('res:', res)

    def test_report2(self):
        context = {}
        values = self.default_get_onchange(context=context)

        date_from = '2020-1-1'
        date_to = '2020-10-10'
        values_input = {'date_from': date_from, 'date_to': date_to}
        values.update(values_input)

        res = self.report_print2(values, context=context)
        print('res:', res)

        res2 = {'doc_ids': [], 'doc_model': 'account.balance.report',
                'data': {'id': 58, 'date_from': '2020-01-01', 'date_to': '2020-10-10',
                         'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'target_move': 'posted', 'company_id': [1, 'My Company'],
                         'used_context': {'journal_ids': [1, 2, 3, 4, 7, 6, 5], 'state': 'posted',
                                          'date_from': '2020-01-01', 'date_to': '2020-10-10',
                                          'strict_range': True,
                                          'company_id': 1, 'lang': 'zh_CN'},
                         'display_account': 'movement'
                         },
                'docs': 'account.balance.report(58,)',
                'time': "<module 'time' (built-in)>",

                'Accounts': [{'credit': 0.0, 'debit': 2290.0, 'balance': 2290.0, 'code': '100201', 'name': '银行'},
                             {'credit': 2290.0, 'debit': 90.0, 'balance': -
                              2200.0, 'code': '112200', 'name': '应收账款'},
                             {'credit': 90.0, 'debit': 0.0, 'balance': -
                              90.0, 'code': '600100', 'name': '主营业务收入'}
                             ]
                }

#


# class ReportPartnerledger(Model):
#     _name = 'report.accounting_pdf_reports.report_partnerledger'

#     def test_report(self):
#         wizard_model = 'account.report.partner.ledger'
#         date_month = '2020-10-1'
#         res = self.report_month(wizard_model, date_month)
#         print('---')
#         for k, v in res.items():
#             print(k, ':', v)

#     def _get_report_values(self, docids, data=None, context=None):
#         return self._get_report_values2(docids, data=data, context=context)
#
    # def report_print3333333(self, values, context=None):
    #     call_fn = self.report_print1
    #     print('-----report_print2, value,', values)

    #     date_from = values.get('date_from')
    #     date_to_init = datetime.strptime(
    #         date_from, '%Y-%m-%d') + relativedelta(days=-1)
    #     date_to_init = date_to_init.strftime('%Y-%m-%d')

    #     values_init = values.copy()
    #     values_init.update({'date_from': False, 'date_to': date_to_init})
    #     res_init = call_fn(values_init, context=context)

    #     values_end = values.copy()
    #     values_end.update({'date_from': False})
    #     res_end = call_fn(values_end, context=context)

    #     res = call_fn(values, context=context)

    #     # print('-----report_print2, res_init,', res_init)
    #     # print('-----report_print2, res_end,', res_end)

    #     def fn1(rec, date_from):
    #         rec2 = rec.copy()
    #         del rec2['lines']
    #         rec2['lines'] = [{
    #             # id = 0 这个是标志
    #             'id': 0, 'date': date_from, 'code': '', 'a_code': '', 'a_name': '', 'ref': None,
    #             # name 可以用来显示
    #             'move_name': 'Initial Balance', 'name': 'Initial Balance',
    #             # 赋值 debit, credit
    #             'debit': rec['debit'], 'credit': rec['credit'],
    #             'amount_currency': 0.0,
    #             'currency_id': 'res.currency()', 'currency_code': None,
    #             # name 可以用来显示
    #             'displayed_name': 'Initial Balance',
    #             # 赋值 progress
    #             'progress': rec['balance']
    #         }]
    #         return rec2

    #     def fn2(rec, date_from):
    #         rec2 = rec.copy()
    #         del rec2['lines']
    #         lines = [ln for ln in rec['lines'] if ln['date'] >= date_from]
    #         rec2['lines'] = lines
    #         return rec2

    #     rpt_init = [fn1(rec, date_from) for rec in res_init['report_data']]
    #     rpt_end = [fn2(rec, date_from) for rec in res_end['report_data']]

    #     res['report_data'] = self.merge_report_data(rpt_init, rpt_end)

    #     # print('-----report_print2, res,', res)

    #     return res

    # def merge_report_data(self, res1, res2):
    #     d = OrderedDict()
    #     for r in res1:
    #         d[r['id']] = r

    #     for r in res2:
    #         rid = r['id']
    #         if rid not in d:
    #             d[rid] = r
    #             continue

    #         nr = r.copy()
    #         nr['lines'] = d[rid]['lines'] + r['lines']
    #         d[rid] = nr

    #     return list(d.values())
